<?php
/**
 * @version $Id: admin.extensionicons2.php 290 2007-12-07 12:19:51Z nik.elkuku $
 * repo $URL: http://joomla-codebase.googlecode.com/svn/trunk/extensionicons2/administrator/components/com_extensionicons2/admin.extensionicons2.php $
 * @package ExtensionIcons2
 * @author Nikolai Plath (elkuku)
 * Created on 14.04.2007
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * WE ARE:
 * !! defined also ins install.php !!
 */
// load the configuration
#ExtensionIcons2::loadConfig( &$etb_CONFIG, $option );
$C['APP_Name']			= 'EasyToolbar2';
$C['APP_Version']		= '0.0 SVN';
$C['APP_com_name']	= 'com_extensionicons2';
$C['APP_mod_name']	= 'mod_extensionicons2';

$configfile = JPATH_ADMINISTRATOR.DS.'components'.DS.$C['APP_com_name'].DS.$C['APP_com_name'].'.config.php';
if( file_exists( $configfile ))
{
	require( $configfile );
} else
{
	echo '<h2 style="color: red">CONFIG NOT FOUND !</h2>';
	echo $configfile.'<hr />';
	return FALSE;
}
if( ! file_exists( JPATH_ADMINISTRATOR.DS.'modules'.DS.$C['APP_mod_name'].DS.$C['APP_mod_name'].'.php'))
{
	echo '<div style="color: red">'.$C['APP_Name'].JText::_(' module NOT FOUND !').'</div>';
	echo JPATH_ADMINISTRATOR.DS.'modules'.DS.$C['APP_mod_name'].DS.$C['APP_mod_name'].'.php';
}
$etb_CONFIG['APP_Name']			= $C['APP_Name'];
$etb_CONFIG['APP_Version']		= $C['APP_Version'];
$etb_CONFIG['APP_com_name']	= $C['APP_com_name'];
$etb_CONFIG['APP_mod_name']	= $C['APP_mod_name'];

$AppVersion = 'ExtensionIcons_2 0.0_SVN';//deprecated

//////////////
/*tiny
 * acl_check
 * we grant access only to Admin and Super Admin
 */
$me = & JFactory::getUser();

if(   $me->usertype != "Administrator"
	&& $me->usertype != "Super Administrator"
	&& $me->username != 'achim'
	)
{
	echo 'no access...';
	#print_r($me);
	return;
}

//dump ($_POST);
global $mainframe;
$cid 		= JRequest::getVar( 'cid', array(0), '', 'array' );
if (!is_array( $cid ))
{
	$cid = array(0);
}

require_once( JApplicationHelper::getPath( 'admin_html' ) );
require_once( JApplicationHelper::getPath( 'class' ) );

if( ! defined( 'NL' ) ) define( 'NL', "\n" );
if( ! defined( 'TB' ) ) define( 'TB', "\t" );

$t_task2 = JRequest::getVar('task2', NULL);

if( $t_task2 == 'saveconfig' )
{
	echo 'saveconf';
	$res = ExtensionIcons2::saveConfig( $etb_CONFIG, 'etb_CONFIG', $option );
	# ) ? JText::_('Configuration saved') : JText::_('There was an error');
  	listItems( $option , $etb_CONFIG );
	$task = '';
} else
{

	$t_task = JRequest::getVar('task');
	$t_task =( $t_task ) ? $task : 'listItems';
	
	switch ( $t_task )
	{
	    case 'exiconRefresh':
	   	  	synchronizeme ( $option , $etb_CONFIG);
	   	  	break;
	   	case 'ConfigModule':
	   		ConfigModule ( $option );
	   		break;
		case 'ConfigExIcons':
	   		showConfig( $etb_CONFIG, $option );
	   		break;
	    case 'listItems':
	      	listItems( $option , $etb_CONFIG );
	      	break;
	    case 'edit':
	      	edit( $option , $cid[0], $etb_CONFIG  );
	      	break;
	    case 'add':
	    	edit( $option , '', $etb_CONFIG );
	    	break;
	    case 'save':
	     	save( $option );
	     	break;
	    case 'remove':
	    	delete( $option , $cid );
	    	break;
	
	    case 'publish':
	     	publish( $option , '1' , $cid );
	     	break;
	    case 'unpublish':
	     	publish( $option , '0' , $cid );
	     	break;
	    case 'orderup':
			orderExIcons( intval( $cid[0] ), -1, $option );
			break;
		case 'orderdown':
			orderExIcons( intval( $cid[0] ), 1, $option );
			break;
		case 'MediaManager':
			//HTML_ExtensionIcons::WrapMediaManager();
			//index.php?option=com_media&task=list&tmpl=component&folder=stories/food
			$mainframe->redirect( "index.php?option=com_media&folder=ExtensionIcons_Custom" );
			break;
		case 'help':
			help ( $option , $etb_CONFIG);
			break;
		case 'cancel'://new in J! 1.5
			echo 'canceled..';
			listItems ( $option, $etb_CONFIG);
		break;
		default:
		echo '<h1>UNDEFINED::)::'.JRequest::getVar('task').'</h1>';
		listItems ( $option, $etb_CONFIG);
		break;
	}//switch
}


/////////////////////////////////////////////////////////////////////////////////////


function listItems( $option , $etb_CONFIG )
{
	global $mainframe;

	$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 0);
	$limitstart = $mainframe->getUserStateFromRequest( $option.'limitstart', 'limitstart', 0 );

	$database = & JFactory::getDBO();//

    $database->setQuery("SELECT COUNT(*) FROM `#__extensionicons2`");
    $total = $database->loadResult();

	if ( $total == 0 )
	{
		echo JText::_('EXICONS_NO_EXICONS');
		return;
	}

	jimport('joomla.html.pagination');
	$pageNav = new JPagination( $total, $limitstart, $limit );

     $database->setQuery("SELECT * FROM `#__extensionicons2`"
                         ." ORDER BY `ordering`", $pageNav->limitstart, $pageNav->limit );
     $rows = $database->loadObjectList();

     HTML_ExtensionIcons::listItems($option, $rows, $pageNav);
     HTML_ExtensionIcons::printFooter( $etb_CONFIG );
}//function

function orderExIcons( $uid, $inc, $option )
{
	global $mainframe;

	$database = & JFactory::getDBO();//
	//$row	=& JTable::getInstance('category' );

	$row = new mosExtensionIcons( $database );
	$row->load( $uid );
	$row->reorder();
	$row->move( $inc, "published >= 0" );
	$row->reorder();

	$mainframe->redirect( 'index2.php?option=' . $option );
}

function publish( $option, $publish ,$cid )
{
	global $mainframe;

	$database = & JFactory::getDBO();//

	$cids = implode( ',', $cid );
    $database->setQuery( "UPDATE `#__extensionicons2`"
                         ."\n SET `published` = '".$publish."'"
                         ."\n WHERE `id` IN (".$cids.")" );
    $database->query();
    $mainframe->redirect( 'index2.php?option=' . $option );
}

function edit( $option, $uid , $etb_CONFIG )
{
	$database = & JFactory::getDBO();//
    $row = new ExtensionIcons2( $database );
    $row->load($uid);
    HTML_ExtensionIcons::edit( $option, $row );
    HTML_ExtensionIcons::printFooter( $etb_CONFIG );
}

function save( $option )
{
print_r($_REQUEST);
die('popo');
	global $mainframe;
	$database = & JFactory::getDBO();
    $row = new ExtensionIcons2( $database );
    $row->bind( $_POST );
    $row->store();
	if( $database->getErrorNum() )
	{
		echo $database->stderr().$database->getErrorMsg();
		return;// TODO error reporting
	}
    $mainframe->redirect( 'index2.php?option='.$option, _EXICONS_SAVED );
}

function delete( $option, $cid )
{
	global $mainframe;
	$database = & JFactory::getDBO();
    $cids = implode( ',', $cid );
    $database->setQuery( "DELETE FROM `#__extensionicons2`"
                         ."\n WHERE `id` IN (".$cids.")" );
    $database->query();
	$mainframe->redirect( 'index2.php?option='.$option );
}

function ConfigModule( $option )
{
	global $mainframe;

	$database = & JFactory::getDBO();
    $database->setQuery("SELECT `id` FROM `#__modules`"
                    ."\n WHERE `module` = 'mod_extensionicons2'");
 	$id = $database->loadResult();
	$mainframe->redirect("index2.php?option=com_modules&client=1&task=edit&hidemainmenu=1&cid[]=".$id);
}

/**
 * editing the configuration file
 */
function showConfig( $etb_CONFIG, $option )
{
#echo 'config exicons..';
#	listItems ( $option , $etb_CONFIG );
     HTML_ExtensionIcons::editConfig(  $option, $etb_CONFIG  );
     
	  HTML_ExtensionIcons::printFooter( $etb_CONFIG );
}// function

/**
 * synchronize with extensions database table
 */
function synchronizeme( $option , $etb_CONFIG)
{
	global $mainframe;

	$database = & JFactory::getDBO();
	echo JText::_('EXICONS_SYNC_START') . ' ';

	$query = "SELECT * FROM `#__extensionicons2`"
				. "\n ORDER BY `id`";
	$database->setQuery( $query );
	$backendicons = $database->loadObjectList();
	
	$database->setQuery("SELECT * FROM `#__components`"
     					."\n WHERE `admin_menu_link` != ''"
                  ."\n ORDER BY `id`");
    $db_components = $database->loadObjectList();
	 $cnt_new = 0;
     foreach( $db_components as $db_component)
     {
     	$found = false;
     	if( count( $db_components > 0 ))
     	{
	     	foreach( $backendicons as $backendicon )
   		  	{
				if( $backendicon->admin_menu_link == $db_component->admin_menu_link )
				{
					$found = true;
				}
     		}// foreach
    	 }
    	 if ($found == false)
    	 {
    	 	$parent_name = '';
    	 	if ($db_component->parent > 0)
    	 	{
    		 	$query = "SELECT name FROM #__components WHERE id = " . $db_component->parent;
   		 	 	$database->setQuery($query);
   		 	 	$parent_name = $database->loadResult();
    		 }
			$query = "INSERT INTO `#__extensionicons2`"
			."\n SET"
			."\n `name` = '".$db_component->name."',"
			."\n `parent` = '".$db_component->parent."',"
			."\n `admin_menu_link` = '".$db_component->admin_menu_link."',"
			."\n `admin_menu_alt` = '".$db_component->admin_menu_alt."',"
			."\n `linkoption` = '".$db_component->option."',"
			."\n `admin_menu_img` = '".$db_component->admin_menu_img."'";

			$query .=( $parent_name != '' ) ? ",\n `parent_name` = '".$parent_name."'" : '';
  		 	$database->setQuery($query);
			$database->query();
			$cnt_new++;
    	 }//if
     }//foreach
	echo $cnt_new . ' ' . JText::_('EXICONS_SYNC_END') . ' ' . '<img src="images/tick.png" />';
	listItems ( $option,$etb_CONFIG );
}//function

/**
 * THE Help Function
 */
function help ( $option , $etb_CONFIG)
{
	// TODO : we will define a basepath until j! dev head
	//			decides about this stuff...
	$BASEPATH = dirname( __FILE__ );
	require ( $BASEPATH.DS.'help'.DS.'german_help.php');
   HTML_ExtensionIcons::printFooter( $etb_CONFIG );
}
?>
